package JMSRaceServer;


import javax.jms.*;
import javax.naming.*;

import raceReservationServer.raceServerGateway.JMSRaceServerGateway;

import JMSRaceServer.dao.JMSRaceServerDAO;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

public class JMSRaceSender {
	public static void main(String[] args)
	{
		String queueName = null;
		Context jndiContext = null;
		QueueConnectionFactory queueConnectionFactory = null;
		QueueConnection queueConnection = null;
		QueueSession queueSession = null;
		Queue queue = null;
		QueueSender queueSender = null;
		TextMessage textMessage = null;
		ObjectMessage objectMessage = null;
		List<RaceDO> races = null;
		JMSRaceServerDAO dao = new JMSRaceServerDAO();
		final int NUM_MSGS;
		
		if ( (args.length < 1) || (args.length > 2) )
		{
			System.out.println("Usage: java SimpleQueueSender <queue-name> [<number-ofmessages>]");
			System.exit(1);
		}
		queueName = new String(args[0]);
		System.out.println("Queue name is " + queueName);
		if (args.length == 2)
		{
			NUM_MSGS = (new Integer(args[1])).intValue();
		}
		else
		{
			NUM_MSGS = 2;
		}
		try
		{
			Properties props = new Properties();
			props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
			"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
			props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
			props.load(new FileInputStream("jndi.properties"));
			jndiContext = new InitialContext(props);
		}
		catch (NamingException e)
		{
			System.out.println("Could not create JNDI API context: " + e.toString());
			System.exit(1);
		}
		catch(IOException ioe)
		{
			System.out.println("Could not find properties file: " + ioe.toString());
			System.exit(1);
		}
		try
		{
			queueConnectionFactory = (QueueConnectionFactory)
			jndiContext.lookup("QueueConnectionFactory");
			queue = (Queue) jndiContext.lookup(queueName);
		}
		catch (NamingException e)
		{
			System.out.println("JNDI API lookup failed: " + e.toString());
			System.exit(1);
		}

		try
		{
			dao.connect();
			races = dao.getRaces();
			dao.disconnect();
		}
		catch (SQLException e1) {e1.printStackTrace();}
		try
		{
			queueConnection = queueConnectionFactory.createQueueConnection();
			queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
			queueSender = queueSession.createSender(queue);
			textMessage = queueSession.createTextMessage();
			objectMessage = queueSession.createObjectMessage();
			
			//create JMSDTO with the list obteined
			for (int i = 0; i < races.size(); i++)
			{
				if ((i % 2) == 0)
				{
					textMessage.setText("This is text message " + (i + 1));
					System.out.println("Sending text message: " + textMessage.getText());
					queueSender.send(textMessage);
				}
				else
				{
					for(int j=0; j < races.size(); j++)
					{
						RaceJMSDTO race = new RaceJMSDTO(races.get(j).getName(), races.get(j).getDistance(), races.get(j).getDate(), races.get(j).getPlaces());
						objectMessage.setObject(race);
						System.out.println("Sending object message: " + race.getName());
						queueSender.send(objectMessage);
					}
				}
			}
			queueSender.send(queueSession.createMessage());
		}
		catch (JMSException e)
		{
			System.out.println("Exception occurred: " + e.toString());
		}
		finally
		{
			if (queueConnection != null)
			{
				try
				{
					queueConnection.close();
				}
				catch (JMSException e) {}
			}
		}
	}
}
